<?php
if(isset($_POST['action']))
{
    $db = new Database();

    $page = $_POST['page'];
    $cur_page = $page;
    $page -= 1;
    $per_page = 5; // Per page
    $previous_btn = true;
    $next_btn = true;
    $first_btn = true;
    $last_btn = true;
    $start = $page * $per_page;
    $action = $_POST['action'];
    $finaldata = "<table border='0' class='table_data table-striped'>
		    <thead>
			<tr>
				<th><center><input type='checkbox' name='selectAll' class='selectAll'/></center></th>
				<th>Image Name</th>
				<th>PortNumber</th>
				<th>Cabinet</th>
				<th>User</th>
				<th>Date Created</th>
				<th>Action</th>
			</tr>
		    </thead>
		    <tbody id='searchDealerName'>";
    if($action=="default")
    {
	$tag = $db->query("SELECT * FROM tag LIMIT $start, $per_page","array");
	$count = $db->query("SELECT * FROM tag","numrows");
        $no_of_paginations = ceil($count / $per_page);
    }elseif($action=="viewCabinetBy"){
        $valueName = $_POST['Name'];
	if($valueName==""){
	    $tag = $db->query("SELECT * FROM tag LIMIT $start, $per_page","array");
	    $count = $db->query("SELECT * FROM tag","numrows");
	}else{
	    $tag = $db->query("SELECT * FROM tag WHERE cabinet='$valueName' LIMIT $start, $per_page","array");
	    $count = $db->query("SELECT * FROM tag WHERE cabinet='$valueName'","numrows");
	}
	// Validation of user logged in the system for viewing their documents according to the credentials.  
	
        $no_of_paginations = ceil($count / $per_page);
	
    }elseif($action=="searchTag"){
	$valueName = $_POST['Name'];
	$tag = $db->query("SELECT * FROM tag WHERE portNumber LIKE '%$valueName%' OR localNumber LIKE '%$valueName%' OR cabinet LIKE '%$valueName%'  LIMIT $start, $per_page","array");
	$count = $db->query("SELECT * FROM tag WHERE portNumber LIKE '%$valueName%' OR localNumber LIKE '%$valueName%' OR cabinet LIKE '%$valueName%'","numrows");
        $no_of_paginations = ceil($count / $per_page);
    }
    if($count>0){
	foreach($tag as $data)
        {
	    $imageID = $data['fileID'];
		$imageName = $db->query("SELECT * FROM file WHERE fileID='$imageID'","row");
	    $userID = $data['userID'];
		$userName = $db->query("SELECT * FROM users WHERE userID='$userID'","row");
	    $finaldata .=   '<tr>
				<td><center><input type="checkbox" name="singleSelectAll[]" class="singleSelectAll" rel="'.$data['id'].'"/></center></td>
				<td>'.$imageName['imageName'].'</td>
				<td>'.$data['portNumber'].'</td>
				<td>'.$data['cabinet'].'</td>
				<td>'.$userName['Firstname']. " " .$userName['Lastname'].'</td>
				<td>'.$data['dateCreated'].'</td>
				<td>
				    <center>
					<img src="/images/icon/view.png" class="tagged cursor" id="'.$data['id'].'" title="View Other Information.">
					<img src="/images/icon/generate_reports.png" width="24px" height="24px" class="gen_rep cursor" data-tag-action="singleTagPrint" rel="'.$data['id'].'" title="Generate Report.">
				    </center>
				</td>
			    </tr>';
        }
        //pagination
        pagination($cur_page,$page,$per_page,$start,$previous_btn,$next_btn,$first_btn,$last_btn,$no_of_paginations,$finaldata,$count,$action,$valueName);
    }else{
        $finaldata.="<tr><td colspan='7'><center>No Record Found</center></td></tr>";
        echo $finaldata;
    }
}
?>
